package com.wskh.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wskh.commonUtil.R;
import com.wskh.edu.entity.Course;
import com.wskh.edu.entity.CourseDescription;
import com.wskh.edu.entity.course.CourseListVo;
import com.wskh.edu.entity.course.CourseSearchObj;
import com.wskh.edu.entity.vo.CourseInfoVo;
import com.wskh.edu.entity.vo.CoursePublishVo;
import com.wskh.edu.service.CourseDescriptionService;
import com.wskh.edu.service.CourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wskh
 * @since 2021-10-14
 */
@RestController
@RequestMapping("/eduService/course")
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE}) // 允许请求跨域
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @ApiOperation(value = "新增课程")
    @PostMapping("saveCourseInfo")
    public R saveCourseInfo(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required =
                    true)
            @RequestBody CourseInfoVo courseInfoVo){
        String courseId = courseService.saveCourseInfo(courseInfoVo);
        if(!StringUtils.isEmpty(courseId)){
            return R.ok().data("courseId", courseId);
        }else{
            return R.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping("getCourseInfoVoByCourseId/{courseId}")
    public R getCourseInfoVoByCourseId(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfoVoByCourseId(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @ApiOperation(value = "修改课程基本信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @ApiOperation(value = "课程最终发布")
    @PostMapping("publishCourse")
    public R publishCourse(@RequestBody CoursePublishVo coursePublish){
        boolean b = courseService.publishCourse(coursePublish);
        if(b){
            return R.ok().message("课程发布成功");
        }else{
            return R.error().message("课程发布失败");
        }
    }

    @ApiOperation(value = "删除课程")
    @PostMapping("deleteCourseById/{id}")
    public R deleteCourseById(@PathVariable String id){
        boolean b = courseService.deleteCourseById(id);
        if(b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "条件分页获取课程列表")
    @PostMapping("getPageListByOption/{page}/{limit}")
    public R getPageListByOption(
            @PathVariable int page,@PathVariable int limit,
            @RequestBody CourseSearchObj searchObj){

        Page<Course> coursePage = new Page<>(page,limit);
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(searchObj.getSubjectId())){
            courseQueryWrapper.eq("subject_id",searchObj.getSubjectId());
        }
        if(!StringUtils.isEmpty(searchObj.getSubjectParentId())){
            courseQueryWrapper.eq("subject_parent_id",searchObj.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(searchObj.getTitle())){
            courseQueryWrapper.like("title",searchObj.getTitle());
        }
        if(!StringUtils.isEmpty(searchObj.getTeacherId())){
            courseQueryWrapper.eq("teacher_id",searchObj.getTeacherId());
        }

        IPage<Course> pageResult = courseService.page(coursePage, courseQueryWrapper);
        List<Course> records = pageResult.getRecords();

        List<CourseListVo> courseListVoList = new ArrayList<>();

        for (Course record : records) {
            CourseListVo courseListVo = new CourseListVo();
            BeanUtils.copyProperties(record,courseListVo);
            courseListVoList.add(courseListVo);
        }

        return R.ok().data("total",pageResult.getTotal()).data("rows",courseListVoList);
    }

    @ApiOperation(value = "根据id获取courseInfo")
    @PostMapping("getCourseById/{id}")
    public R getCourseById(@PathVariable String id){
        // 要返回的对象
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        // 获取课程信息
        Course course = courseService.getById(id);
        BeanUtils.copyProperties(course,courseInfoVo);
        // 获取描述信息
        QueryWrapper<CourseDescription> courseDescriptionQueryWrapper = new QueryWrapper<>();
        courseDescriptionQueryWrapper.eq("id",id);
        CourseDescription courseDescription = courseDescriptionService.getOne(courseDescriptionQueryWrapper);
        //
        courseInfoVo.setDescription(courseDescription.getDescription());
        return R.ok().data("items",courseInfoVo);
    }

    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourse")
    public R updateCourse(@RequestBody CoursePublishVo coursePublish){
        System.out.println(coursePublish);
        boolean b = courseService.updateCourse(coursePublish);
        if(b){
            return R.ok().message("更新成功");
        }else{
            return R.error().message("更新失败");
        }
    }
}

